package com.apes.hr.paySupport.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.plugin.soe.metadata.BusinessAttributeValue;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.hr.base.model.Employee;
import com.apes.hr.base.model.Job;
import com.apes.hr.base.model.WorkPost;
import com.apes.hr.transfer.model.TransferApplicationItem;
import com.apes.scm.masterdata.enumeration.model.Enumeration;
import com.apes.scm.masterdata.frame.model.Branch;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;

/**
 * @Title: SupportApplicationExecution
 * @Description:付费支援申请单执行表
 * @author: ZhengZeXian
 * @create: 2021/4/15 10:10
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = SupportApplicationExecution.CONST_TABLE_NAME)
public class SupportApplicationExecution extends Model {

    public static final String CONST_TABLE_NAME = "hr_support_execution";

    @Id
    @Column(name = "id",nullable = false, length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "付费支援申请单执行表编码")
    @Comment("编码")
    private String id;

    @OneToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "supportApplication_item_id",  nullable = false)
    private SupportApplicationItem supportApplicationItem;

    @Column(name = "start_date")
    @Comment("生效日期")
    private Date startDate;

    @Column(name = "end_date")
    @Comment("支援结束日期")
    private Date endDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "start_time",referencedColumnName = "id")
    @Comment("支援开始时间")
    private Enumeration startTime; // AM为上午 PM为下午

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "end_time",referencedColumnName = "id")
    @Comment("支援结束时间")
    private Enumeration endTime; // AM为上午 PM为下午

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "support_uid", referencedColumnName = "id")
    @Comment("人员编码")
    private Employee supportUid;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "branch_id", referencedColumnName = "id")
    @Comment("调动后部门")
    private Branch branch;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "post_id", referencedColumnName = "id")
    @Comment("调动后岗位")
    private WorkPost workPost;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "job_id", referencedColumnName = "id")
    @Comment("调动后职位")
    private Job job;

    @Column(name = "candidates", length = 4000)
    @Comment("出人门店店长OA候选人")
    private String candidates;

    @Column(name = "support_days", length = 50, nullable = false)
    @Comment("支援天数")
    private String supportDays;

    @Column(name = "toll_case",columnDefinition = ApesConst.FIELD_BOOLEAN_FALSE)
    @Comment("收费情况标识")
    private boolean tollCase = false;   //1、免费，2、收费

    @Column(name = "toll_amt", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("收费金额标准/天")
    private double tollAmt = 0;

    @Column(name = "discounts_amt", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("实际收费金额/天")
    private double discountsAmt = 0;

//    @Column(name = "toll_total_amt", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
//    @Comment("总计收费金额")
//    private double tollTotalAmt = 0;

//    @Column(name = "discounts_total_amt", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
//    @Comment("总计优惠金额")
//    private double discountsTotalAmt = 0;

    @Column(name = "actual_pay_amt", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("实际需支付总金额")
    private double actualPayAmt = 0;

    //表单前置
    @Column(name = "end_date_pre")
    @Comment("支援结束日期")
    private Date endDatePre;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "end_time_pre",referencedColumnName = "id")
    @Comment("支援结束时间")
    private Enumeration endTimePre; // AM为上午 PM为下午

    @Transient
    private String sign;

    @Transient
    private String processInitiator;

    @Transient
    private String supportDate;

    /**
     * OA 流程 备注里的 支援日期
     */
    public String getSupportDate() {
        Date endDate = this.getEndDatePre() == null ? this.endDate : this.getEndDatePre();
        Enumeration endTime = this.getEndTimePre() == null ? this.getEndTime() : this.getEndTimePre();
        return DateUtil.format(startDate,DateUtil.SHOW_DATE_FORMAT) + startTime.getDescription() + " — "
                + DateUtil.format(endDate,DateUtil.SHOW_DATE_FORMAT) + endTime.getDescription();
    }
}
